/*
 * @Author       : 邱狮杰
 * @Date         : 2021-09-08 15:27:58
 * @LastEditTime : 2021-09-09 12:43:19
 * @FilePath     : /smallnewhope/src/utils/deviceMotionHandler.ts
 * @Description  :
 */
import Shake from 'shake.js'
function startListen(cb: Function) {
	if (window.DeviceMotionEvent) {
		window.addEventListener("devicemotion", cb as any, false);
	} else {
		alert("改机型不支持摇一摇功能，请点击下方按钮");
	}
}
// @ts-ignore
export function getPermission(cb: Function): { stop: Function, remove: Function } {
	// // @ts-ignore
	// const s = new Shake({
	// 	threshold: 10, // optional shake strength threshold
	// 	timeout: 500 // optional, determines the frequency of event generation
	// })
	// s.start()
	// window.addEventListener('shake', cb as any, false);
	// return {
	// 	stop: s.stop,
	// 	remove: () => window.removeEventListener('shake', cb as any, false)

	// }
	if (
		typeof window.DeviceMotionEvent !== 'undefined' &&
		// @ts-ignore
		typeof window.DeviceMotionEvent.requestPermission === 'function'
	) {
		// @ts-ignore
		window.DeviceMotionEvent.requestPermission()
			// @ts-ignore
			.then(function (state) {
				if ('granted' === state) {
					//用户同意授权
					cb()
				} else {
					//用户拒绝授权
					alert('摇一摇需要授权设备运动权限,请重启应用后,再次进行授权!')
				}
			})
			// @ts-ignore
			.catch(function (err) {
				alert(err)
				// @ts-ignore
				alert('error: ' + err)
			})
	}
}


